/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.DistributionListSubscriptionPolicy;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.DistributionListUnsubscriptionPolicy;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Group.GroupOwner;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.account.type.DistributionListSubscribeOp;
/*     */ import com.zimbra.soap.account.type.DistributionListSubscribeStatus;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SubscribeDistributionList
/*     */   extends DistributionListDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  60 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  61 */     Provisioning prov = Provisioning.getInstance();
/*  62 */     Account acct = getRequestedAccount(zsc);
/*     */     
/*  64 */     if (!canAccessAccount(zsc, acct)) {
/*  65 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*     */     
/*  68 */     Element response = zsc.createElement(AccountConstants.SUBSCRIBE_DISTRIBUTION_LIST_RESPONSE);
/*     */     
/*  70 */     Group group = getGroupBasic(request, prov);
/*  71 */     SubscribeDistributionListHandler handler = new SubscribeDistributionListHandler(group, request, response, prov, acct);
/*     */     
/*  73 */     handler.handle();
/*     */     
/*  75 */     return response;
/*     */   }
/*     */   
/*     */   private static class SubscribeDistributionListHandler extends DistributionListDocumentHandler.SynchronizedGroupHandler
/*     */   {
/*     */     private final Element request;
/*     */     private final Element response;
/*     */     private final Provisioning prov;
/*     */     private final Account acct;
/*     */     
/*     */     protected SubscribeDistributionListHandler(Group group, Element request, Element response, Provisioning prov, Account acct)
/*     */     {
/*  87 */       super();
/*  88 */       this.request = request;
/*  89 */       this.response = response;
/*  90 */       this.prov = prov;
/*  91 */       this.acct = acct;
/*     */     }
/*     */     
/*     */     protected void handleRequest() throws ServiceException
/*     */     {
/*  96 */       DistributionListSubscribeOp op = DistributionListSubscribeOp.fromString(this.request.getAttribute("op"));
/*     */       
/*     */ 
/*  99 */       String[] members = { this.acct.getName() };
/*     */       
/* 101 */       DistributionListSubscribeStatus status = null;
/* 102 */       boolean accepted = false;
/* 103 */       if (op == DistributionListSubscribeOp.subscribe) {
/* 104 */         ZAttrProvisioning.DistributionListSubscriptionPolicy policy = this.group.getSubscriptionPolicy();
/*     */         
/* 106 */         if (policy == ZAttrProvisioning.DistributionListSubscriptionPolicy.ACCEPT) {
/* 107 */           DistributionListDocumentHandler.addGroupMembers(this.prov, this.group, members);
/* 108 */           accepted = true;
/* 109 */           status = DistributionListSubscribeStatus.subscribed;
/* 110 */         } else { if (policy == ZAttrProvisioning.DistributionListSubscriptionPolicy.REJECT) {
/* 111 */             throw ServiceException.PERM_DENIED("subscription policy for group " + this.group.getName() + " is reject");
/*     */           }
/*     */           
/* 114 */           SubscribeDistributionList.ApprovalRequestSender sender = new SubscribeDistributionList.ApprovalRequestSender(this.prov, this.group, this.acct, op, null);
/* 115 */           sender.composeAndSend();
/* 116 */           status = DistributionListSubscribeStatus.awaiting_approval;
/*     */         }
/*     */       }
/*     */       else {
/* 120 */         ZAttrProvisioning.DistributionListUnsubscriptionPolicy policy = this.group.getUnsubscriptionPolicy();
/*     */         
/* 122 */         if (policy == ZAttrProvisioning.DistributionListUnsubscriptionPolicy.ACCEPT) {
/* 123 */           DistributionListDocumentHandler.removeGroupMembers(this.prov, this.group, members);
/* 124 */           accepted = true;
/* 125 */           status = DistributionListSubscribeStatus.unsubscribed;
/* 126 */         } else { if (policy == ZAttrProvisioning.DistributionListUnsubscriptionPolicy.REJECT) {
/* 127 */             throw ServiceException.PERM_DENIED("un-subscription policy for group " + this.group.getName() + " is reject");
/*     */           }
/*     */           
/* 130 */           SubscribeDistributionList.ApprovalRequestSender sender = new SubscribeDistributionList.ApprovalRequestSender(this.prov, this.group, this.acct, op, null);
/* 131 */           sender.composeAndSend();
/* 132 */           status = DistributionListSubscribeStatus.awaiting_approval;
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 137 */       if (accepted) {
/* 138 */         ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "SubscribeDistributionList", "name", this.group.getName(), "op", op.name(), "member", Arrays.deepToString(members) }));
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 144 */       this.response.addAttribute("status", status.name());
/*     */     }
/*     */   }
/*     */   
/*     */   private static class ApprovalRequestSender
/*     */     extends DistributionListDocumentHandler.NotificationSender
/*     */   {
/*     */     private ApprovalRequestSender(Provisioning prov, Group group, Account requestingAcct, DistributionListSubscribeOp op)
/*     */     {
/* 153 */       super(group, requestingAcct, op);
/*     */     }
/*     */     
/*     */     private void composeAndSend() throws ServiceException
/*     */     {
/* 158 */       List<String> owners = new ArrayList();
/*     */       
/* 160 */       Group.GroupOwner.getOwnerEmails(this.group, owners);
/*     */       
/* 162 */       if (owners.size() == 0) {
/* 163 */         throw ServiceException.PERM_DENIED(this.op.name() + "request needs approval but there is no owner for list " + this.group.getName());
/*     */       }
/*     */       
/*     */ 
/* 167 */       sendMessage((String[])owners.toArray(new String[owners.size()]));
/*     */     }
/*     */     
/*     */     private void sendMessage(String[] owners) throws ServiceException {
/*     */       try {
/* 172 */         SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
/*     */         
/* 174 */         Address fromAddr = AccountUtil.getFriendlyEmailAddress(this.requestingAcct);
/*     */         
/* 176 */         Address replyToAddr = fromAddr;
/* 177 */         String replyTo = this.requestingAcct.getAttr("zimbraPrefReplyToAddress");
/* 178 */         if (replyTo != null) {
/* 179 */           replyToAddr = new JavaMailInternetAddress(replyTo);
/*     */         }
/*     */         
/*     */ 
/* 183 */         out.setFrom(fromAddr);
/*     */         
/*     */ 
/* 186 */         out.setReplyTo(new Address[] { replyToAddr });
/*     */         
/*     */ 
/* 189 */         List<Address> addrs = Lists.newArrayList();
/* 190 */         for (String ownerEmail : owners) {
/* 191 */           addrs.add(new JavaMailInternetAddress(ownerEmail));
/*     */         }
/* 193 */         out.addRecipients(Message.RecipientType.TO, (Address[])addrs.toArray(new Address[addrs.size()]));
/*     */         
/*     */ 
/* 196 */         out.setSentDate(new Date());
/*     */         
/*     */ 
/* 199 */         Locale locale = getLocale(this.requestingAcct);
/*     */         
/*     */ 
/* 202 */         String subject = L10nUtil.getMessage(L10nUtil.MsgKey.dlSubscriptionRequestSubject, locale, new Object[0]);
/* 203 */         out.setSubject(subject);
/*     */         
/* 205 */         buildContentAndSend(out, locale, "group subscription request");
/*     */       }
/*     */       catch (MessagingException e) {
/* 208 */         ZimbraLog.account.warn("send share info notification failed, rcpt='" + Arrays.deepToString(owners) + "'", e);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */     protected MimeMultipart buildMailContent(Locale locale)
/*     */       throws MessagingException
/*     */     {
/* 216 */       String text = textPart(locale);
/* 217 */       String html = htmlPart(locale);
/* 218 */       String xml = xmlPart(locale);
/*     */       
/*     */ 
/* 221 */       MimeMultipart mmp = new ZMimeMultipart("alternative");
/*     */       
/*     */ 
/* 224 */       MimeBodyPart textPart = new ZMimeBodyPart();
/* 225 */       textPart.setText(text, "utf-8");
/* 226 */       mmp.addBodyPart(textPart);
/*     */       
/*     */ 
/* 229 */       MimeBodyPart htmlPart = new ZMimeBodyPart();
/* 230 */       htmlPart.setDataHandler(new DataHandler(new DistributionListDocumentHandler.NotificationSender.HtmlPartDataSource(html)));
/* 231 */       mmp.addBodyPart(htmlPart);
/*     */       
/*     */ 
/* 234 */       MimeBodyPart xmlPart = new ZMimeBodyPart();
/* 235 */       xmlPart.setDataHandler(new DataHandler(new DistributionListDocumentHandler.NotificationSender.XmlPartDataSource(xml)));
/* 236 */       mmp.addBodyPart(xmlPart);
/*     */       
/* 238 */       return mmp;
/*     */     }
/*     */     
/*     */     private String textPart(Locale locale) {
/* 242 */       StringBuilder sb = new StringBuilder();
/*     */       
/* 244 */       L10nUtil.MsgKey msgKey = DistributionListSubscribeOp.subscribe == this.op ? L10nUtil.MsgKey.dlSubscribeRequestText : L10nUtil.MsgKey.dlUnsubscribeRequestText;
/*     */       
/*     */ 
/* 247 */       sb.append("\n");
/* 248 */       sb.append(L10nUtil.getMessage(msgKey, locale, new Object[] { this.requestingAcct.getName(), this.group.getName() }));
/*     */       
/* 250 */       sb.append("\n\n");
/* 251 */       return sb.toString();
/*     */     }
/*     */     
/*     */     private String htmlPart(Locale locale) {
/* 255 */       StringBuilder sb = new StringBuilder();
/*     */       
/* 257 */       sb.append("<h4>\n");
/* 258 */       sb.append("<p>" + textPart(locale) + "</p>\n");
/* 259 */       sb.append("</h4>\n");
/* 260 */       sb.append("\n");
/* 261 */       return sb.toString();
/*     */     }
/*     */     
/*     */     private String xmlPart(Locale locale) {
/* 265 */       StringBuilder sb = new StringBuilder();
/*     */       
/* 267 */       String URI = "urn:zimbraDLSubscription";
/* 268 */       String VERSION = "0.1";
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 273 */       String groupDisplayName = this.group.getDisplayName();
/* 274 */       groupDisplayName = groupDisplayName == null ? "" : groupDisplayName;
/*     */       
/* 276 */       String userDisplayName = this.requestingAcct.getDisplayName();
/* 277 */       userDisplayName = userDisplayName == null ? "" : userDisplayName;
/*     */       
/* 279 */       sb.append(String.format("<%s xmlns=\"%s\" version=\"%s\" action=\"%s\">\n", new Object[] { "dlSubs", "urn:zimbraDLSubscription", "0.1", this.op.name() }));
/*     */       
/* 281 */       sb.append(String.format("<dl id=\"%s\" email=\"%s\" name=\"%s\"/>\n", new Object[] { this.group.getId(), this.group.getName(), groupDisplayName }));
/*     */       
/* 283 */       sb.append(String.format("<user id=\"%s\" email=\"%s\" name=\"%s\"/>\n", new Object[] { this.requestingAcct.getId(), this.requestingAcct.getName(), userDisplayName }));
/*     */       
/* 285 */       sb.append(String.format("</%s>\n", new Object[] { "dlSubs" }));
/*     */       
/* 287 */       return sb.toString();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/SubscribeDistributionList.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */